#include "key.h"

uint8_t key_read(void)
{
	if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_0) == 0)
		return 1;
	else if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_1) == 0)
		return 2;
	else if(HAL_GPIO_ReadPin(GPIOB,GPIO_PIN_2) == 0)
		return 3;
	else if(HAL_GPIO_ReadPin(GPIOA,GPIO_PIN_0) == 0)
		return 4;
	else
		return 0;
}

uint32_t key_old_time;
uint8_t key_now,key_down,key_old,key_up;
uint8_t select;//0:PD | 1: PH|2: PX
uint32_t PD_T = 1000, PH_T = 5000;
int PX_T;
void key_proc(void)
{
	key_now = key_read();
	key_down = key_now & (key_now ^ key_old);
	key_up = ~key_now & (key_now ^ key_old);
	key_old = key_now;

	if (key_down)
	{
		key_old_time = HAL_GetTick();
		switch(key_down)
		{
			//+
		case 1:

			if(show_mode == 1)
			if (select == 0)
			{
				PD += 100;
				if (PD > 1000)
					PD = 1000;
			}
			else if (select == 1)
			{
				PH += 100;
				if (PH > 10000)
					PH = 10000;
			}
			else if (select == 2)
			{
				PX += 100;
				if (PX > 1000)
					PX = 1000;
			}

			break;
			//-
		case 2:
			if (show_mode == 1)
				if (select == 0)
				{
					PD -= 100;
					if (PD < 100)
						PD = 100;
				}
				else if (select == 1)
				{
					PH -= 100;
					if (PH < 1000)
						PH = 1000;
				}
				else if (select == 2)
				{
					PX -= 100;
					if (PX < -1000)
						PX = -1000;
				}
			break;
		case 3:
			if (show_mode == 1)
			{
				select++;
				if (select > 2)
				{
					select = 0;
				}
			}
			else if (show_mode == 0)
			{
				show_mode_small ^= 1;
			}


			break;
		case 4:
			show_mode++;
			if (show_mode > 2)
				show_mode = 0;
			if(show_mode == 0)
			{
				show_mode_small = 0;
			}
			if(show_mode == 1)
			{
				select = 0;
			}
			if(show_mode == 2)
			{
				PD_T = PD;
				PH_T = PH;
				PX_T = PX;
			}
			break;
		default:
			break;
		}
	}
	uint32_t now_time = HAL_GetTick();
	if (key_up && now_time>=key_old_time+1000)
	{
		if(key_up == 3)
		{
			if (show_mode == 2)
			{
				NA[0] = 0;
				NA[1] = 0;
				NB[0] = 0;
				NB[1] = 0;
			}		

		}
	}
}